package ro.unibuc.nautilus.controllers;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Required;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import ro.unibuc.nautilus.domain.Employee;
import ro.unibuc.nautilus.service.EmployeesService;

public class EmployeesController extends MultiActionController {
	
	EmployeesService employeesService;
	
	public ModelAndView employees(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		ModelAndView mav = new ModelAndView("employees");
		mav.addObject("employees",  employeesService.findAll());
		return mav;
	}
	
	public ModelAndView removeFromCrew(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		long employeeId = ServletRequestUtils.getLongParameter(request, "emplId", -1);
		if (employeeId == -1) {
			return null;
		}
		Employee empl = employeesService.findEmployee(employeeId);
		
		Long shipId = empl.getShip().getId();
		empl.setShip(null);
		
			
		employeesService.createOrUpdate(empl);
		
		return new ModelAndView("redirect:crewedit.htm?shipId=" + shipId);
	}

    @Required
    public void setEmployeesService(EmployeesService employeesService) {
        this.employeesService = employeesService;
    }
	
}
